package com.linkage.educloud.ah.adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.VClassBean;

import java.util.List;

/**
 * Created by Mr'Tang on 2018/12/11.
 */

public class VClassAdapter extends android.widget.BaseAdapter {
    List<VClassBean> mList;
    Context mContext;

    public VClassAdapter(Context context, List<VClassBean> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_vclass, null);
            holder = new ViewHolder();
            holder.ac_vclass_free = convertView.findViewById(R.id.ac_vclass_free);
            holder.ac_vclass_bar = convertView.findViewById(R.id.ac_vclass_bar);
            holder.ac_vclass_bar_text = convertView.findViewById(R.id.ac_vclass_bar_text);
            holder.ac_vclass_used = convertView.findViewById(R.id.ac_vclass_used);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        VClassBean info = (VClassBean) getItem(position);
        holder.ac_vclass_free.setText(Html.fromHtml(info.getChildschool()
                + info.getChildless() + "\t\t<font color ='green'>每月免费"
                + info.getTotaltime() + "分钟</font>"));

        double pro = 0.0;
        int progress = 0;
        if (info.getTotaltime() > 0) {
            pro = (Math.round(info.getUserdtime() * 100) / (100.0 * info.getTotaltime()));
            progress = (int) Math.ceil((info.getUserdtime() * 100 * 100) / (100.0 * info.getTotaltime()));
        }
        holder.ac_vclass_used.setText("已使用" + info.getUserdtime() + "分钟，剩余" + (info.getTotaltime() - info.getUserdtime()) + "分钟");
        holder.ac_vclass_bar_text.setText(pro * 100 + "%");
        holder.ac_vclass_bar.setProgress(progress);
        return convertView;
    }

    private class ViewHolder {
        TextView ac_vclass_free;
        ProgressBar ac_vclass_bar;
        TextView ac_vclass_bar_text;
        TextView ac_vclass_used;
    }
}
